rm(list=ls())
d <- read.csv("data-yougov-initial.csv")


# FigureB5 -----------------------------------------------------------------------------------------------

RvT <- lm_robust(treatT ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$voteTory),])
RvL <- lm_robust(treatL ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$voteTory),])

RvT.d <- data.frame(summary(RvT)$coefficients[rownames(summary(RvT)$coefficients)!="(Intercept)",])
RvL.d <- data.frame(summary(RvL)$coefficients[rownames(summary(RvL)$coefficients)!="(Intercept)",])

randcheck1 <- ggplot(RvT.d, aes(as.factor(rownames(RvT.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(a) Randomization Check: Predictors of Treatment 1") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) +
  coord_flip() + ylim(-1,1)

randcheck2 <- ggplot(RvL.d, aes(as.factor(rownames(RvL.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(b) Randomization Check: Predictors of Treatment 2") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) + 
  scale_x_discrete(labels=NULL) + 
  coord_flip() + ylim(-1,1) 
ggarrange(randcheck1, randcheck2)
ggsave("FigureB5.pdf", width = 15, height = 6, units = "in")


# FigureB4  -----------------------------------------------------------------


voteT.Tory   <- lm_robust(voteTory ~ factor(orderA) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
voteT.Labour <- lm_robust(voteTory ~ factor(orderA) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

voteT <- c(voteT.Tory$coefficients["treatT"], voteT.Labour$coefficients["treatT"])
se   <- c(voteT.Tory$std.error["treatT"], voteT.Labour$std.error["treatT"])
party <- c("Tory", "Labour")
data3 <- data.frame(voteT, se, party)

voteL.Tory   <- lm_robust(voteLabour ~ factor(orderA) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
voteL.Labour <- lm_robust(voteLabour ~ factor(orderA) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

voteL <- c(voteL.Tory$coefficients["treatL"], voteL.Labour$coefficients["treatL"])
se   <- c(voteL.Tory$std.error["treatL"], voteL.Labour$std.error["treatL"])
party <- c("Tory", "Labour")
data4 <- data.frame(voteL, se, party)


expT <- ggplot(data = data3, aes(x = voteT, y = as.factor(party) )) +
  geom_pointrange(aes(xmin=voteT-1.96*se, xmax=voteT+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(a) Effect of Most Similar Tory Treatment on Voting")+ ylab("Voter Partisanship")  + theme_minimal() + 
  theme(axis.text = element_text(size=14),
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title.y = element_text(angle=90, hjust=.5, margin = margin(0,40,0,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.45,.5) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 

expL <- ggplot(data = data4, aes(x = voteL, y = as.factor(party) )) +
  geom_pointrange(aes(xmin=voteL-1.96*se, xmax=voteL+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(b) Effect of Most Similar Labour Treatment on Voting")+ ylab(" ")  + theme_minimal() + 
  theme(axis.text = element_text(size=14), 
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.4,.5) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 

ggarrange(expT, expL)
ggsave("FigureB4.pdf", width = 15, height = 6, units = "in")



